/**
 * Created with hong.rong
 * Date:2020/07/27
 */

import { useState, useEffect } from 'react'
import { getBannerList } from '@/services/config/banner'

export default function useBanner() {
  const [loading, setLoading] = useState(false)
  const [dataSource, setDataSource] = useState([])
  const [pagination, setPagination] = useState({
    page: 1,
    pageSize: 10,
  })
  const [total, setTotal] = useState(0)

  useEffect(() => {
    async function getList() {
      await handleFetchList(pagination)
    }
    getList()
  }, [])

  /**
   * 获取列表
   * @param params
   * @returns {Promise<void>}
   */
  const handleFetchList = async (params = {}) => {
    setLoading(true)
    setPagination(params)
    const { data = {} } = await getBannerList(params)
    if (data && data.code === 0) {
      setDataSource(data.data || [])
      setTotal(data.totalCount || 0)
      setLoading(false)
    } else {
      setLoading(false)
    }
  }

  return {
    loading,
    pagination,
    total,
    dataSource,
    handleFetchList,
  }
}
